package com.shengqin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shengqin.common.pojo.CommonPage;
import com.shengqin.entity.School;
import com.shengqin.entity.User;
import com.shengqin.mapper.UserMapper;
import com.shengqin.service.ISchoolService;
import com.shengqin.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author Rui.Zhou
 * @since 2023-12-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public Page<User> getPage(CommonPage<User> pageParam) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User userParam = pageParam.getQueryParam();
        if (StringUtils.hasLength(userParam.getName())) {
            wrapper.lambda().like(User::getAccount, userParam.getName());
        }
//        if(userParam.getCreateUserId() != null){
        wrapper.lambda().eq(User::getCreateUserId, StpUtil.getLoginId());
//        }
        wrapper.orderByDesc("id");
        Page<User> userPage = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
        page(userPage, wrapper);
        return userPage;
    }

    @Override
    public User getByAccount(String account) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getAccount, account);
        return getOne(wrapper);
    }

    @Override
    public List<User> listByRoleId(Integer roleId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getRoleId, roleId);
        return list(wrapper);
    }
}
